//
//  HTUserAccountTools.swift
//  WeiBoDemo
//
//  Created by 郝涛 on 16/4/20.
//  Copyright © 2016年 郝涛. All rights reserved.
//

import UIKit

class HTUserAccountTools: NSObject {

   //设置全局访问点
    static let sharedTools:HTUserAccountTools = HTUserAccountTools()
    
    
    //UserInfoModel
    var UserInfoModel:HTUserInfoModel?
    
    //判断用户是否登录
    var isLogin:Bool{
        if accessToken != nil{
            
            return true
        }else{
            return false
        }
    }
    
    
    //token
    
    var accessToken :String?{
        //没有过期
        if !isExpires{
            //返回accessToken
            return UserInfoModel?.access_token
        }else{
            
            return nil
        }
        
    }
    
    //expires_Date 判断访问令牌是否过期
    
    var isExpires:Bool{
        //获取过期时间
        let date = UserInfoModel?.expires_Date
        //获取到当前时间进行比较，
        if date?.compare(NSDate()) == NSComparisonResult.OrderedDescending{// 降序
            //过期了
            return false
        }else{
            //没过期
            return true
            
        }
        
    }
    
    //拼接路径
    let path = (NSSearchPathForDirectoriesInDomains(.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask, true).last!as NSString).stringByAppendingPathComponent("ahah.account")
    
    override init() {
        super.init()
        UserInfoModel = getUserAccount()
    }
    //保存数据
    func saverAccount(userInfo:HTUserInfoModel){
        
       UserInfoModel = userInfo
        
        NSKeyedArchiver.archiveRootObject(userInfo, toFile: path)
    }
    
    //读取数据
    func getUserAccount()->HTUserInfoModel? {
        
        
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(path)as? HTUserInfoModel
        
        return result
    }
}
